<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="xinzeng"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button size="small" type="success" @click="fenpei(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="bianji(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryParams.page"
                :page-sizes="[2,3,5,10,]"
                :page-size="queryParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
              </el-pagination>
            </el-row>
            <!-- 对话框 -->
            <el-dialog :title="isEdit ? '编辑' : '添加' " 
                @close="hclose"
                :close-on-click-modal="false"
                :close-on-press-escape="false"
                :visible.sync="showDialog">
                <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
                  <el-form-item label="角色名称" prop="name">
                    <el-input v-model="roleForm.name" />
                  </el-form-item>
                  <el-form-item label="角色描述" prop="description">
                    <el-input v-model="roleForm.description" />
                  </el-form-item>
                </el-form>
                <!-- 底部 -->
                <el-row slot="footer" type="flex" justify="center">
                  <el-col :span="6">
                    <el-button size="small" @click="showDialog = false">取消</el-button>
                    <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
                  </el-col>
                </el-row>
              </el-dialog>
              <!-- 分配权限对话框 -->
              <el-dialog title="分配权限" :visible.sync="quanxianDialog">
               <quanxian :id="userId" @success="quxiao"/>
              </el-dialog>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
  import {getRoleList,deleRole,addRole,barRole} from '@/api/settings'
import quanxian from './quanxian.vue'
export default {
  components: { quanxian },
  data() {
    return {
      roleForm:{
        name:'',
        description:''
      },
      queryParams:{
        page:1,
        pagesize:3
      },
      total:0 ,
      list:[] , // 存储角色的列表
      showDialog:false , // 对话框显示隐藏
      isEdit:false,  // true为编辑,false为新增
      quanxianDialog:false,
      userId:'' , // 保存员工Id
      // 校验
      rules:{
        name:[
       {required:true,message:'请输入内容',trigger:'blur'},
      ],
      description:[
       {required:true,message:'请输入内容',trigger:'blur'}
      ]
      }
    }
  },

  created(){
   this.loadRoleList()
  },
  methods: {
    fenpei(id){
      this.userId = id
     this.quanxianDialog = true
    },
    quxiao(){
     this.quanxianDialog = false
    },
    // 重置表单
    hclose(){
     console.log('close.......弹层关闭了');
     this.roleForm={
        name:'',
        description:''
      }
      this.$refs.roleForm.resetFields()
    },
    // 编辑请求
    async bianRole(){
     try {
      const res =  await barRole(this.roleForm)
      this.loadRoleList()
      this.showDialog = false
     } catch (error) {
      console.log(error);
     }
    },
    // 新增请求
    async loadRole(){
    const res = await addRole(this.roleForm)
    console.log(res,'新增角色');
    this.total++
    const maxPage = Math.ceil(this.total / this.queryParams.pagesize)
    this.queryParams.page = maxPage
    this.loadRoleList()
    this.showDialog = false
    this.$refs.roleForm.resetFields()
    },
    // 新增按钮
    xinzeng(){
      this.showDialog = true
      this.isEdit = false
    },
    // 编辑按钮
   async bianji(row){
      this.showDialog = true
      this.isEdit = true
      this.roleForm = {...row}
    },
    // 点击确定表单检验,判断是添加或者编辑
    hSubmit(){
      this.$refs.roleForm.validate(valid=>{
       if(!valid) return
       this.isEdit ? this.bianRole() : this.loadRole()
      })
     
    },
    // 用户点击了删除
   async hDel(row){
     const res = await deleRole(row.id)
     if(this.list.length === 1){
            this.queryParams.page--
            if(this.queryParams.page <= 0){
             this.queryParams.page = 1
            }
          }
     this.$message.success(res.message)
     this.loadRoleList()
    },
    // 分页控制 
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      // 改变queryParams.pagesize每页显示的条数
      this.queryParams.page = 1
      this.queryParams.pagesize = val
      this.loadRoleList()
    },
    // 当前的页码值进行切换时触发 val===> 页码值
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryParams.page = val
      this.loadRoleList()
    },
  // 获取所有角色列表
  async loadRoleList(){
    try {
      const res  = await getRoleList(this.queryParams)
      console.log(res,'角色列表');
      this.list = res.data.rows
      this.total = res.data.total
    } catch (error) {
      console.log(error);
    }
  }
  
  }
}
</script>
